import {Http} from '@angular/http';
import {NgLayer, NgLayerRef} from 'ng2-layer';
import {Component,ViewContainerRef, Injectable}  from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {FormsModule}  from '@angular/forms';
/**
 * 左侧菜单部分
 */
@Component({
	selector: '.box_category',
	templateUrl: 'template/router/boxProduct/category.html'
})
export class BoxCategoryComp {
	categories=[];
	products=[];
	
	constructor(
	private http:Http,
	router:ActivatedRoute,
	private layer:NgLayer,
	private parent:ViewContainerRef){
		//拖动分类排序
		dragDropSort({
			axis:"Y", 		//指定元素只能沿x轴或者y轴移动
			sortable:true,
			dragItems:".category_item:not(.category_header)",	//可拖动元素的选择器
			sortContainer:".categories",
			
			drop: function(a, item) {
				a.classList.remove("hover");
				if(a!=item.parentNode){
					a.appendChild(item)
				}
			}
		});
		
		router.parent.params.subscribe(params => {
			this.boxId = params.id;
			
			//加载产品分类
			http.get({
				search:{
					m:"Home",
					c:"ApiPublic",
					a:"getGoodsTypeByBoxId",
					box_id:this.boxId
				}
			}).then((data)=>{
				this.categories = data.data;
			});
			//加载商品列表
			this.loadProduct();
		});
	}
	
	/**
	 * 删除分类
	 */
	deleteCategory(ev, cate){
		ev.stopPropagation();
		
		let ly = this.layer.confirm({
			message:"确定要删除该分类么?"
		}).ok(()=>{
			this.http.get({
				search:{
					m:"Admin",
					c:"ApiGoodsType",
					a:"deleteType",
					type_id:cate.type_id
				}
			}).then(()=>{
				this.categories = this.categories.filter((c)=>{
					return !(c.type_id==cate.type_id);
				});
				ly.close();
			})
		}).cancel(()=>{
			ly.close();
		});
	}
	
	//加载商品列表
	loadProduct(cate){
		this.cateId = cate?cate.type_id:null;
		this.http.get({
			search:{
				m:"Admin",
				c:"ApiGoods",
				a:"getGoodsList",
				box_id:this.boxId,
				type:this.cateId
			}
		}).then((data)=>{
			this.products = data.data.goods_data;
		});
	}
	
	//弹窗添加分类
	addCategory(){
		this.layer.dialog({
			title:"添加分类",
			dialogComponent:AddCategoryComp,
			parent:this.parent,
			data:{parent:this},
			imports:[FormsModule]
		});
	}
	
	/**
	 * 选择分类
	 */
	selectType(good, cate, e){
		if(!e.target.checked){
			good.typeInfo = good.typeInfo.filter((i)=>{
				return i.type_id!=cate.type_id;
			})
		} else {
			good.typeInfo.push(cate);
		}
		this.updateGoodCategory(good);
	}
	
	/**
	 * 更新产品分类
	 */
	updateGoodCategory(good){
		this.http.post({
			search:{
				m:"Admin",
				c:"ApiGoods",
				a:"updateGoods",
				goods_id:good.goods_id,
				box_id:this.boxId
			},
			body:{
				types:JSON.stringify(good.typeInfo.map((t)=>{
					return t.type_id
				}))
			}
		}).then((data)=>{
			
		})
	}
	
	/**
	 * 编辑分类
	 */
	editCategory(e, cate){
		e.stopPropagation();
		
		this.layer.dialog({
			title:"添加分类",
			dialogComponent:EditCategoryComp,
			parent:this.parent,
			data:{category:cate, boxId:this.boxId},
			imports:[FormsModule]
		});
	}
}

@Component({
	templateUrl:"template/router/boxProduct/addCategory.html"
})
class AddCategoryComp {
	parent:any;
	
	constructor(private ly:NgLayerRef, private http:Http){}
	
	//
	save(){
		this.http.post({
			search:{
				m:"Admin",
				c:"ApiGoodsType",
				a:"addGoodsType"
			},
			body:{
				name:this.name,
				box_id:this.parent.boxId
			}
		}).then((data)=>{
			this.parent.categories.push(data.data);
			this.ly.close();
		})
	}
}

/**
 * 
 */
@Component({
	templateUrl:"template/router/boxProduct/editCategory.html"
})
class EditCategoryComp {
	category:any;
	boxId:any;
	constructor(private ly:NgLayerRef, private http:Http){}
	
	ngOnInit(){
		this.tempCate = Object.assign({}, this.category);
	}
	
	save(){
		this.http.post({
			search:{
				m:"Admin",
				c:"ApiGoodsType",
				a:"updateType"
			},
			body:{
				name:this.tempCate.name,
				type_id:this.category.type_id
			}
		}).then((data)=>{
			Object.assign(this.category, this.tempCate)
			this.ly.close();
		})
	}
}